# -*- coding: utf-8 -*-
import sys
from datetime import datetime

from conf_funs import get_funs, is_time_run
from select_phone import select_phone
from weixin_modules.init_logger import init_logger

serialno, device_name = select_phone()
logger = init_logger(serialno, device_name)

from weixin_modules.other_fun import *
from weixin_modules.weixin import WeixinTest
import settings
from conf_funs import is_time_ready


# 处理业务配置，

weixin = WeixinTest(serialno)
funs_dict = get_funs(weixin.wxid)

def auto_run():
    while is_time_run(settings.global_end_time_hour):
        # 功能 1 群发消息
        if funs_dict["flag_mass_message"]:
            weixin.mass_message()
        # 功能2，临时，改好友的备注名称，
        if funs_dict["flag_change_friends_notes"]:
            weixin.auto_change_friends_note(settings.runtime_auto_change_friends_note)
        # 功能3，查看信息流，
        if funs_dict["flag_auto_view_information_flow"]:
            weixin.auto_view_information_flow(settings.runtime_auto_view_information_flow)
        # 功能4，分析群地区，
        if funs_dict["flag_auto_get_rooms_area"]:
            weixin.auto_get_rooms_area(settings.runtime_auto_get_rooms_area)
        # 功能5 ,临时,改群备注,
        if funs_dict["flag_auto_change_rooms_notes"]:
            weixin.auto_change_rooms_notes(settings.runtime_auto_change_rooms_notes)
        # 功能6，自动接受好友邀请，添加好友
        if funs_dict["flag_auto_accept_friend_invitations"]:
            weixin.auto_accept_friend_invitations()
        # 功能7， 取消群折叠，保存到通讯录，
        if funs_dict["flag_auto_room_Unfold_saveTo_book"]:
            weixin.auto_room_Unfold_saveTo_book(settings.runtime_auto_room_Unfold_saveTo_book)
        if funs_dict["flag_clean_rcoard"]:
            # 每天定时执行，
            if is_time_ready(settings.clean_rcoard_time):
                weixin.clean_rcoard()
                sys.exit("程序运行结束：")

        logger.debug(f"等待时间（秒）：{settings.waiting_time}")
        # 功能5 等待，让手机停一下，防止过热，卡顿，
        flag_time = 0
        deadline = time.time() + settings.waiting_time
        while time.time() < deadline:
            time.sleep(4)
            flag_time += 4
            print("{} >> ".format(flag_time), end="")
        print("\n")


def init_user():
    weixin.init_user()

def get_friends_info():
    weixin.get_friends_info()


def get_rooms_area():
    weixin.get_rooms_area()


def room_hide_save_to_book():
    weixin.room_hide_saveTo_book()


def auto_run_auto():
    auto_run()


def clean_rcoard():
    weixin.clean_rcoard()


def temp_get_rooms_area():
    weixin.temp_get_rooms_area()


def temp_get_friends_info_slow():
    weixin.temp_get_friends_info_slow()


def auto_room_unfold_save_to_book(timeout):
    weixin.auto_room_Unfold_saveTo_book(timeout)


# 定义一个字典，将选项映射到函数
options_map = {
    1: init_user,
    2: get_rooms_area,
    3: room_hide_save_to_book,
    4: auto_run_auto,  # 如果逻辑相同，可以删除重复的选项
    5: clean_rcoard,
    6: temp_get_rooms_area,
    7: temp_get_friends_info_slow,
    8: auto_room_unfold_save_to_book,
    9: get_friends_info,
    10: None  # 如果选项10不需要执行任何操作，可以赋值为None
}

# 主循环
while True:
    option = weixin.select_option()
    if option in options_map:
        func = options_map[option]
        if func:
            func()  # 执行对应的函数
        else:
            print("No action for this option.")
    else:
        print("Invalid option.")
